package com.bitdf.txing.search.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.to.es.SkuEsModel;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.search.service.MallProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RequestMapping("${txing.request.path.search}/mall")
@RestController
@Slf4j
public class MallProductSaveController {
    @Autowired
    MallProductSaveService productSaveService;

    /**
     * 商品上架
     * @param skuEsModels
     * @return
     */
    @SaCheckLogin
    @SaCheckPermission("mall:search:product:up")
    @PostMapping("/product/up")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean b = false;
        try {
            b = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("商品上架错误", e);
            return R.error(TxCodeEnume.PRODUCT_UP_FAILURE_EXCEPTION);
        }
        if (b) {
            log.info("商品成功上架");
            return R.ok();

        } else {
            log.info("商品上架失败");
            return R.error(TxCodeEnume.PRODUCT_UP_FAILURE_EXCEPTION);
        }
    }
}
